<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>删除列表项</title>
    <script src="../js/vue.js"></script>
    <style>
        .fade-enter-active,
        .fade-leave-active {
            transition: opacity .5s;
        }

        .fade-enter,
        .fade-leave-to
        /* .fade-leave-active below version 2.1.8 */
            {
            opacity: 0;
        }
    </style>
</head>

<body>
    <!-- 挂载点 -->
    <div id="app">
        <!-- 标签，提示用户 输入学号和姓名 -->
        <label for="StuId">学号</label>
        <input type="text" v-model="tempId" id="StuId"></input>
        <label for="StuName">姓名</label>
        <input type="text" v-model="tempName" id="StuName"></input>
        <!-- 添加按钮 -->
        <button v-on:click="addItem">添加</button>
        <!-- 列表渲染 -->
        <h1>基础列表渲染</h1>
        <ul>
            <!-- <li v-for="item in items" :key="item.id" v-on:click="items.splice(index,1)"></li> -->
            <transition-group name="fade">
                <li v-for="(item, index) in items" :key="item.id" v-on:click="del(index)">
                    学号: {{item.id }} --- 姓名: {{item.name}}
                </li>
            </transition-group>  
        </ul>
    </div>

    <!-- 使用javascript 脚本 -->
    <script type="text/javascript">
        var vm = new Vue({
            // 挂载点
            el: "#app",
            // 数据
            data: {
                // 临时变量，用于接收用户输入的值
                tempId: "",
                tempName: "",
                items: [
                    { id: 202319180601, name: "陈全扬" },
                    { id: 202319180602, name: "陈严标" },
                    { id: 202319180603, name: "陈宇" },
                    { id: 202319180604, name: "陈宇航" },
                ],
            },
            // 方法
            methods: {
                addItem() {
                    // 添加数据
                    if (this.tempId && this.tempName) {
                        this.items.push({ id: this.tempId, name: this.tempName });
                        // 清空输入框
                        this.tempId = "";
                        this.tempName = "";
                    }
                },
                del(index) {
                    // 删除数据
                    this.items.splice(index, 1);
                }
            },
        });
    </script>
</body>

</html>